
var id = getUrlParam("id");
var data;
$.ajax({
  url: "/admin/orders/get/" + id,
  async: false,
  dataType: "json",
  success: function(res) {
    data = res.result;
  }
});
var tpl = document.getElementById("tpl");
if (data.ordersState == 1) {
  var html = template("noPay", {});
  tpl.innerHTML = html;
  document.getElementById("cancel").onclick = function() {
    var btnArray = ['是', '否'];
    mui.confirm('您确认要取消订单？', '取消订单', btnArray, function(e) {
      if (e.index == 0) {
        updateState(7);
      }
    })

  };
}
if (data.ordersState == 2) {

  var html = template("payed", {});
  tpl.innerHTML = html;
   leftTimer(Date.parse(new Date())+4*60*60*1000);
   document.getElementById("agress").onclick = function() {
     var btnArray = ['是', '否'];
     mui.confirm('您确认要同意预约？', '同意预约', btnArray, function(e) {
       if (e.index == 0) {
         updateState(3);
       }
     })

   };
  //console.log(Date.parse(new Date()));
}

if (data.ordersState == 3) {
  var html = template("use", {});
  tpl.innerHTML = html;

}

if (data.ordersState == 4) {
  var html = template("used", {});
  tpl.innerHTML = html;
}

if (data.ordersState == 5) {
  var html = template("refunding", {});
  tpl.innerHTML = html;
}

if (data.ordersState == 6) {
  var html = template("refunded", {});
  tpl.innerHTML = html;

}

if (data.ordersState == 7) {
  var html = template("cancelTpl", {});
  tpl.innerHTML = html;
}




var main = document.getElementById("main");
var html = template("mainTpl", data);
main.innerHTML = html;

function updateState(state) {
  $.ajax({
    url: "/admin/orders/update?spid=" + id + "&state=" + state,
    async: false,
    dataType: "json",
    success: function(res) {
      if (res.code == 200) {
        mui.toast("操作成功");
        setTimeout(function() {
          location.reload();
        }, 1000);

      }
    }
  });
}

function leftTimer(timestamp) {
  var leftTime = timestamp - Date.parse(new Date()); //计算剩余的毫秒数
  var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
  var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
  var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
  var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
  days = checkTime(days);
  hours = checkTime(hours);
  minutes = checkTime(minutes);
  seconds = checkTime(seconds);
  setInterval("leftTimer("+timestamp+")", 1000);
  document.getElementById("downTime").innerHTML =  hours + ":" + minutes + ":" + seconds ;
}

function checkTime(i){ //将0-9的数字前面加上0，例1变为01
 if(i<10)
 {
  i = "0" + i;
 }
 return i;
}
